package hn.cch.redisson.configuration;

import hn.cch.redisson.constant.RedissonConstant;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.BaseConfig;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.redisson.config.MasterSlaveServersConfig;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;


@Configuration
public class RedissonConfiguration {


    private <T extends BaseConfig<T>> void setPassword(BaseConfig<T> baseConfig, String password) {
        if (StringUtils.hasLength(password)) {
            baseConfig.setPassword(password);
        } else {
            baseConfig.setPassword(null);
        }
    }

    /**
     * 单机
     *
     * @return RedissonClient
     */
    @Bean(destroyMethod = "shutdown")
    public RedissonClient singleServer() {

        Config config = new Config();
        SingleServerConfig singleServerConfig = config.useSingleServer();
        singleServerConfig.setAddress(String.format("redis://%s:%d", RedissonConstant.host, RedissonConstant.port));
        singleServerConfig.setDatabase(RedissonConstant.database);
        setPassword(singleServerConfig, RedissonConstant.password);
        return Redisson.create(config);
    }

    /**
     * 集群
     *
     * @return RedissonClient
     */
    // @Bean
    public RedissonClient clusterServers() {
        String nodeAddress = String.format("redis://%s:%d", RedissonConstant.host, RedissonConstant.port);
        Config config = new Config();
        ClusterServersConfig clusterServersConfig = config.useClusterServers();
        clusterServersConfig.addNodeAddress(nodeAddress);
        setPassword(clusterServersConfig, RedissonConstant.password);
        return Redisson.create(config);
    }

    /**
     * 主从
     *
     * @return RedissonClient
     */
    // @Bean
    public RedissonClient masterSlaveServers() {
        String masterAddress = String.format("redis://%s:%d", RedissonConstant.host, RedissonConstant.port);
        String[] slaveAddress = new String[]{"", ""};
        Config config = new Config();
        MasterSlaveServersConfig masterSlaveServersConfig = config.useMasterSlaveServers();
        masterSlaveServersConfig.setMasterAddress(masterAddress);
        masterSlaveServersConfig.addSlaveAddress(slaveAddress);
        masterSlaveServersConfig.setDatabase(RedissonConstant.database);
        setPassword(masterSlaveServersConfig, RedissonConstant.password);
        return Redisson.create(config);
    }

    /**
     * 哨兵
     *
     * @return RedissonClient
     */
    // @Bean
    public RedissonClient sentinelServers() {
        String sentinelAddress = String.format("redis://%s:%d", RedissonConstant.host, RedissonConstant.port);
        Config config = new Config();
        SentinelServersConfig sentinelServersConfig = config.useSentinelServers();
        sentinelServersConfig.addSentinelAddress(sentinelAddress);
        sentinelServersConfig.setDatabase(RedissonConstant.database);
        if (StringUtils.hasLength(RedissonConstant.password)) {
            sentinelServersConfig.setPassword(RedissonConstant.password);
        } else {
            sentinelServersConfig.setPassword(null);
        }
        return Redisson.create(config);
    }
}
